var j = {
    slice: "slice",
    symbol: "symbol",
    layerGroup: "layer-group",
    bitmap: "bitmap",
    hotspot: "hotspot",
    text: "text",
    shape: "shape",
    path: "path"
}

function o(t) {
    if (Array.isArray(t)) {
        for (var e = 0, n = new Array(t.length); e < t.length; e++)
            n[e] = t[e];
        return n
    }

    if (Symbol.iterator in Object(t) || "[object Arguments]" === Object.prototype.toString.call(t))
        return Array.from(t)

    throw new TypeError("Invalid attempt to spread non-iterable instance")
}

// 蓝湖 Object(it.c)(window.layerBox.layerTree.levelMapToArray())
// I(window.layerBox.layerTree.levelMapToArray())
function I(t) {
    var e = {}
      , n = {};
    (t = t.filter((function(t, n) {
        return e[t.id] = {
            data: t,
            children: []
        },
        t.originIndex = n,
        !t.isCopy || t.type === j.layerGroup && t.hasOwnProperty("symbolID")
    }
    ))).forEach((function(t) {
        t.parentID ? e[t.parentID] && e[t.parentID].children.unshift(t) : e.root = {
            data: t,
            children: []
        }
    }
    )),
    t.forEach((function(t) {
        var i, o = null === (i = e[t.parentID]) || void 0 === i ? void 0 : i.data;
        o && !function t(e, n) {
            if (e.image)
                return !0;
            var i, o = null === (i = n[e.parentID]) || void 0 === i ? void 0 : i.data;
            return !!o && t(o, n)
        }(o, e) && (n[t.id] = t)
    }
    ));
    console.log("t: " + t.length);
    var i = Object.values(n);
    i.sort((function(t, e) {
        return t.originIndex - e.originIndex
    }
    ));
    console.log("i: " + i.length);
    var r = (i = i.filter((function(t) {
        return t.image,
        !0
    }
    ))).filter((function(t) {
        return t.type === j.slice
    }
    ))
      , a = new Set;
      console.log("r: " + r.length);
    return r.forEach((function(t) {
        (function t(e) {
            var n, i = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : {}, o = arguments.length > 2 ? arguments[2] : void 0;
            return (null === (n = e.children) || void 0 === n ? void 0 : n.length) && e.children.forEach((function(e) {
                i[e.id] = e,
                t(o[e.id], i, o)
            }
            )),
            Object.values(i)
        }
        )(e[t.parentID], {}, e).filter((function(e) {
            return e && (i = e,
            (n = t).left <= i.left && n.top <= i.top && n.left + n.width >= i.left + i.width && n.top + n.height >= i.top + i.height);
            var n, i
        }
        )).forEach((function(t) {
            // console.log("a: " + a.size),
            a.add(t)
        }
        ))
    }
    )),
    i = i.filter((function(t) {
        return !o(a).some((function(e) {
            return t.id === e.id
        }
        )) || t.image
    }
    )),
    i = [e.root.data].concat(o(i))
}

function filterCopy(t) {
    return t.filter((function(t, e) {
        return t.originIndex = e,
        !t.isCopy
    }
    ));
}

/// 修正ps数据，映射为sketch格式
function fixPsData(dataList) {
    for (var i = 0; i < dataList.length; i++) {
        let data = dataList[i];
        data.width = data.width / 2;
        data.height = data.height / 2;
        data.x = data.left / 2;
        data.y = data.top / 2;

        if (data.type == "textLayer") {
            data.type = "Text";
            data.font = {
                content: data.textInfo.text,
                font: data.textInfo.fontPostScriptName,
                styles: [{
                    color: data.textInfo.color
                }],
                size: data.textInfo.size / 2
            }
            data.opacity = 100;
        } else if (data.type == "shapeLayer") {
            data.type = "shape";
            if (data.fill) {
                data.fills = [data.fill];
                if (data.fill.gradient) {
                    let gradient = data.fill.gradient;
                    gradient.colorStops = gradient.colors;
                    if (gradient.transparency && gradient.transparency.length > 0) {
                        gradient.from = {
                            x: gradient.transparency[0].location / 4096,
                            y: gradient.transparency[0].midpoint / 4096,
                        };
                        gradient.to = {
                            x: gradient.transparency[gradient.transparency.length - 1].location / 4096,
                            y: gradient.transparency[gradient.transparency.length - 1].midpoint / 4096,
                        }
                    }
                }
            }
            if (data.blendOptions && data.blendOptions.opacity) {
                data.opacity = data.blendOptions.opacity.value;
            } else {
                data.opacity = 100;
            }
            if (data.path && data.path.pathComponents && data.path.pathComponents.length > 0) {
                let path = data.path.pathComponents[data.path.pathComponents.length - 1];
                if (path.origin && path.origin.radii) {
                    data.radius = path.origin.radii.map((function(r) { return r / 2 }));
                }
            }
        } else if (data.images) {
            data.image = data.images;
        }
    }

    return dataList;
}

// const fs = require('fs');
// const level = require('./detail_part.js');
// const obs = require('./object.js');
// function readJSONFile() {
//     const fileData = fs.readFileSync("./sketch.json", 'utf-8');
//     const jsonData = JSON.parse(fileData);
//     if (jsonData.info) {
//         console.log("jsonData.info: " + jsonData.info.length)
//     }

//     let icfe = level.initNative();
//     for (key in icfe) {
//         console.log(key, icfe[key]);
//     }

//     let map = icfe.b(jsonData.info)
//     console.log("map: " + map.length)

//     let notCopy = filterCopy(jsonData.info);
//     console.log("notCopy: " + notCopy.length)

//     let result = I(obs.layerTree)
//     console.log("result: " + result.length)

// }
// readJSONFile();